'use strict';

const Controller = require('egg').Controller;

class ListController extends Controller {
    //新增
    async add() {
        let { ctx,service } = this;
        // ctx.get('role')  身份
        //0.**********判断你是否有权限操作这个接口***********
        //1.获取前端传递的参数
        //2.容错处理
        //3.添加
        let role = ctx.get('role');
        if(role === 'v0' || role === 'v1'){
            let create_time = new Date(),
            end_type = '安卓';
            let {phone,nick_name,tags,role,id_card,address,info} = ctx.request.body;
            if(phone && nick_name && tags && id_card && address &&info && role){
                try{    
                    await service.list.add(phone,nick_name,tags,role,id_card,address,info,create_time,end_type);
                    ctx.body = {
                        code:1,
                        msg:'添加成功'
                    }
                }catch(e){
                    ctx.body = {
                        code:0,
                        msg:e
                    }
                }
            }else{
                ctx.body = {
                    code:2,
                    msg:'参数有误'
                }
            }
        }else{
            ctx.body = {
                code:5,
                msg:'没有权限'
            }
        }
    }

    //编辑

    async edit() {
        let { ctx,service } = this;
        // ctx.get('role')  身份
        //0.**********判断你是否有权限操作这个接口***********
        //1.获取前端传递的参数
        //2.容错处理
        //3.添加
        let role = ctx.get('role');
    
        if(role === 'v0' || role === 'v1'){
            let create_time = new Date();
            let {phone,nick_name,tags,role,id_card,address,info,id} = ctx.request.body;
            if(phone && nick_name && tags && id_card && address &&info && role){
                try{    
                    await service.list.edit(phone,nick_name,tags,role,id_card,address,info,create_time,id);
                    ctx.body = {
                        code:1,
                        msg:'修改成功'
                    }
                }catch(e){
                    ctx.body = {
                        code:0,
                        msg:e
                    }
                }
            }else{
                ctx.body = {
                    code:2,
                    msg:'参数有误'
                }
            }
        }else{
            ctx.body = {
                code:5,
                msg:'没有权限'
            }
        }
    }

    //删除
    async del(){
        let {ctx,service} = this;
        //判断权限
        console.log("ctxparams ===del",ctx.params);
        let {ids} = ctx.query;
        console.log(ids);
        if(ctx.get('role') === 'v0'){
            if(ids){
                try{
                    await service.list.del(ids);
                    ctx.body = {
                        code:1,
                        msg:'删除成功'
                    }
                }catch(e){
                    ctx.body = {
                        code:0,
                        msg:'删除失败'
                    }
                }
            }else{
                ctx.body = {
                    code:2,
                    msg:'参数有误'
                }
            }
        }else{
            ctx.body = {
                code:5,
                msg:'没有权限'
            }
        }
    }
    //查询
    async list(){
        let {ctx,service} = this;
        let list = await service.list.list();
        ctx.body = {
            code:1,
            data:list
        }
    }
}

module.exports = ListController;
